// PolicyCompute_v4

import pc3 from "./PolicyCompute_v3.5.js";
import dayjs from "dayjs";

export default function (cellData, step = 4) {
  // 天府单个小时进港 ≥ 40
  // 天府连续两个小时进港 ≥ 36
  // 天府连续两个半点进港 ≥ 24
  // 则...
  // 实施独立进近 -> (扇区 +1, Max.8)
  cellData = pc3(cellData, step);
  if (!cellData || cellData.length == 0) {
    return cellData;
  } else {
    for (let i = 0; i < cellData.length; i++) {
      let arr = cellData[i].airport.ZUTF.type.ARR.total;
      let arr_last = 0;
      let arr_next = 0;
      // let arr_half = 0;
      // let arr_half_next = 0;
      if (step == 2 && i < cellData.length - 1) {
        arr += cellData[i + 1].airport.ZUTF.type.ARR.total;
      } else if (step == 1 && i < cellData.length - 3) {
        arr += cellData[i + 1].airport.ZUTF.type.ARR.total;
        arr += cellData[i + 2].airport.ZUTF.type.ARR.total;
        arr += cellData[i + 3].airport.ZUTF.type.ARR.total;
      }
      if (step == 4) {
        if (i > 0) {
          arr_last += cellData[i - 1].airport.ZUTF.type.ARR.total;
        }
        if (i < cellData.length - 1) {
          arr_next += cellData[i + 1].airport.ZUTF.type.ARR.total;
        }
      } else if (step == 2) {
        if (i > 1) {
          arr_last += cellData[i - 1].airport.ZUTF.type.ARR.total;
          arr_last += cellData[i - 2].airport.ZUTF.type.ARR.total;
        }
        if (i < cellData.length - 3) {
          arr_next += cellData[i + 2].airport.ZUTF.type.ARR.total;
          arr_next += cellData[i + 3].airport.ZUTF.type.ARR.total;
        }
      } else if (step == 1) {
        if (i > 3) {
          arr_last += cellData[i - 1].airport.ZUTF.type.ARR.total;
          arr_last += cellData[i - 2].airport.ZUTF.type.ARR.total;
          arr_last += cellData[i - 3].airport.ZUTF.type.ARR.total;
          arr_last += cellData[i - 4].airport.ZUTF.type.ARR.total;
        }
        if (i < cellData.length - 7) {
          arr_next += cellData[i + 4].airport.ZUTF.type.ARR.total;
          arr_next += cellData[i + 5].airport.ZUTF.type.ARR.total;
          arr_next += cellData[i + 6].airport.ZUTF.type.ARR.total;
          arr_next += cellData[i + 7].airport.ZUTF.type.ARR.total;
        }
      }
      //
      cellData[i].sectorReason =
        cellData[i].sectorReason +
        ", (前" +
        arr_last +
        "/中" +
        arr +
        "/后" +
        arr_next +
        ")";
      cellData[i]["extInfo"] = {
        standalone: 0,
        pms: 0,
        hourFlow: {
          ZUTF: {
            ARR: {
              last: arr_last,
              current: arr,
              next: arr_next,
            },
          },
        },
      };
      let hour = dayjs(cellData[i].timestamp).hour();
      if (hour == 9) {
        cellData[i].extInfo.pms = 1;
        let sr = Math.min(cellData[i].sectorRequire + 1, 8);
        if (step == 4) {
          cellData[i].sectorRequire = sr;
          cellData[i].sectorReason += "PMS";
        } else if (step == 2 && i < cellData.length - 1) {
          cellData[i].sectorRequire = sr;
          cellData[i].sectorReason += "PMS";
          cellData[i + 1].sectorRequire = sr;
          cellData[i + 1].sectorReason += "PMS";
        } else if (step == 1 && i < cellData.length - 3) {
          cellData[i].sectorRequire = sr;
          cellData[i].sectorReason += "PMS";
          cellData[i + 1].sectorRequire = sr;
          cellData[i + 1].sectorReason += "PMS";
          cellData[i + 2].sectorRequire = sr;
          cellData[i + 2].sectorReason += "PMS";
          cellData[i + 3].sectorRequire = sr;
          cellData[i + 3].sectorReason += "PMS";
        }
      }
      if (
        hour >= 8 &&
        hour < 19 &&
        (arr >= 40 ||
          (i > 0 && (arr_last >= 36 || arr_next >= 36) && arr >= 36))
      ) {
        cellData[i].extInfo.standalone = 1;
        let sr = Math.min(cellData[i].sectorRequire + 1, 8);
        let remark = "独立进近+0";
        if (sr > cellData[i].sectorRequire) {
          remark = "独立进近+1";
        }
        cellData[i].sectorRequire = sr;
        cellData[i].sectorReason = cellData[i].sectorReason + remark;
        if (step == 2 && i < cellData.length - 1) {
          cellData[i + 1].sectorRequire = sr;
          cellData[i + 1].sectorReason = cellData[i + 1].sectorReason + remark;
        } else if (step == 1 && i < cellData.length - 3) {
          cellData[i + 1].sectorRequire = sr;
          cellData[i + 1].sectorReason = cellData[i + 1].sectorReason + remark;
          cellData[i + 2].sectorRequire = sr;
          cellData[i + 2].sectorReason = cellData[i + 2].sectorReason + remark;
          cellData[i + 3].sectorRequire = sr;
          cellData[i + 3].sectorReason = cellData[i + 3].sectorReason + remark;
        }
      }
    }
    // console.log("PolicyCompute_v4", cellData);
    return cellData;
  }
}
